package com.ctvit.framework.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Condition {
	private List items=new ArrayList();
	private String prefix;

	public Condition(){
		this(null);
	}
	public Condition(String prefix){
		this.prefix=prefix;
	}
	public static Condition newInstance(){
		return new Condition();
	}
	public static Condition newInstance(String prefix){
		return new Condition(prefix);
	}	
	public Condition add(String propertyName,Object value){
		add(propertyName,"=",value,"and");
		return this;
	}
	public Condition add(String propertyName,String op,Object value){
		add(propertyName,op,value,"and");
		return this;
	}
	public Condition add(String propertyName,String op,Object value,boolean isRaw){
		add(propertyName,op,value,"and",isRaw);
		return this;
	}	
	public Condition add(String propertyName,String op,Object value,String prefix){
		add(propertyName,op,value,prefix,false);
		return this;
	}
	public Condition add(String propertyName,String op,Object value,String prefix,boolean isRaw){
		if(items.size()==0){
			items.add(new Expression(propertyName,op,value,null,isRaw));
		}else{
			items.add(new Expression(propertyName,op,value,prefix,isRaw));
		}
		return this;
	}
	public Condition add(Condition condition){
		items.add(condition);
		return this;
	}
	public List expand(){
		ArrayList list=new ArrayList();
		list.add(new Expression(null,null,"(",this.prefix,true));
		for(Iterator iterator=items.iterator();iterator.hasNext();){
			Object item=iterator.next();
			if(item instanceof Expression) {
				list.add(item);
			}else if(item instanceof Condition){
				list.addAll(((Condition)item).expand());
			}
		}
		list.add(new Expression(null,null,")",this.prefix,true));
		return list;
	}
	/**
	 * �ж��Ƿ��в�ѯ����
	 * @return false:����.
	 * 		   true:��
	 */
	public boolean isEmpty()
	{
		if (items==null||items.size()==0)return true;
		return false;
	}
}
